package com.cainaer.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cainaer.product.domain.Comment;
import com.cainaer.product.mapper.CommentMapper;
import com.cainaer.product.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author Tina
 * @since 2020-09-11
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 新增评论数据
     *
     * @param comment
     * @return
     */
    @Override
    public Integer insertCommentReply(Comment comment) {
        return commentMapper.insert(comment);
    }

    /**
     * 编辑评论
     *
     * @param comment
     * @return
     */
    @Override
    public Integer editComment(Comment comment) {
        return commentMapper.updateById(comment);
    }


    /**
     * 根据条件查询评论 分页
     *
     * @param comment
     * @return
     */
    @Override
    public IPage<Comment> selectComment(Comment comment, Integer pageNum, Integer pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("show_status", 0);
        Page<Comment> iPage = new Page<Comment>(pageNum, pageSize);
        return commentMapper.selectPage(iPage, queryWrapper);
    }

    /**
     * 通过商商品ID查询这个这个商品的平均评价级别
     *
     * @param productId 商品ID
     * @return
     */
    @Override
    public double selectCommentAvgLevelByProductId(Long productId) {
        return commentMapper.selectCommentAvgLevelByProductId(productId);
    }
}
